package org.devio.secondlevelmenu;

import android.os.Bundle;
import android.widget.Toast;


import org.devio.secondlevelmenu.bean.VipShareFirstLebalBean;
import org.devio.secondlevelmenu.bean.VipShareSecondLeftLebalBean;
import org.devio.secondlevelmenu.bean.VipShareSecondRightLebalBean;
import org.devio.secondlevelmenu.view.VipInfoTwoLevelTabBar;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

import static org.devio.secondlevelmenu.view.VipInfoTwoLevelTabBar.NO_SELECT;
import static org.devio.secondlevelmenu.view.VipInfoTwoLevelTabBar.ONE_LEBAL;
import static org.devio.secondlevelmenu.view.VipInfoTwoLevelTabBar.TWO_LABEL;

public class MainActivity extends AppCompatActivity {


    private VipInfoTwoLevelTabBar levelTabbar;
    // 二级标签 的 左侧标签 数据
    List<VipShareSecondLeftLebalBean> vipShareSecondLeftLebalBeans = new ArrayList<>();
    List<VipShareSecondRightLebalBean> rightLebalBeans1Confirm = new ArrayList<>();
    List<VipShareSecondRightLebalBean> rightLebalBeans2Confirm = new ArrayList<>();
    String[] rightTabs = {"",""};
    String[] confirmrightTabs = {"",""};
    List<VipShareSecondRightLebalBean> rightLebalBeans1 = new ArrayList<>();
    List<VipShareSecondRightLebalBean> rightLebalBeans2 = new ArrayList<>();
    private int selectedLeftId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        levelTabbar = findViewById(R.id.level_tab_bar);
        levelTabbar.setShowTopTags(NO_SELECT);
        levelTabbar.setChooseLebalListener(new VipInfoTwoLevelTabBar.ChooseLebalListener() {
            @Override
            public void selectFirstLabel(VipShareFirstLebalBean firstLebalBean) {
                Toast.makeText(MainActivity.this,
                        firstLebalBean.getTxt(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void selectSecondLeftLabel(VipShareSecondLeftLebalBean secondLeftLebalBean) {
                // 每次切换 左边标签 都要重新加载右侧标签的数据源
                Toast.makeText(MainActivity.this, "selectSecondLeftLabel", Toast.LENGTH_SHORT).show();
                levelTabbar.setClickable(false);
                if (vipShareSecondLeftLebalBeans.indexOf(secondLeftLebalBean) == selectedLeftId){
                    if (rightLebalBeans1Confirm.size()==0)
                    levelTabbar.setSecondRightLebalBeansAll(null, rightLebalBeans2Confirm);
                    else if(rightLebalBeans2Confirm.size()==0)
                        levelTabbar.setSecondRightLebalBeansAll(rightLebalBeans1Confirm, null);
                    else
                        levelTabbar.setSecondRightLebalBeansAll(rightLebalBeans1Confirm, rightLebalBeans2Confirm);
                }else
                    levelTabbar.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setSecondRightTags(vipShareSecondLeftLebalBeans.indexOf(secondLeftLebalBean));

                        }
                    }, 1000);

            }

            @Override
            public void confirmSecondRightLabels(VipShareSecondLeftLebalBean secondLeftLebalBean, VipShareSecondRightLebalBean secondRightLebalBean) {
                if (secondLeftLebalBean == null) {
                    rightLebalBeans1Confirm.clear();
                    rightLebalBeans2Confirm.clear();
                    Toast.makeText(MainActivity.this, "nothing", Toast.LENGTH_SHORT).show();
                    selectedLeftId = -1;
                } else if (secondRightLebalBean == null) {
                    rightLebalBeans1Confirm.clear();
                    rightLebalBeans2Confirm.clear();
                    rightLebalBeans1Confirm.addAll(rightLebalBeans1);
                    rightLebalBeans2Confirm.addAll(rightLebalBeans2);
                    selectedLeftId = vipShareSecondLeftLebalBeans.indexOf(secondLeftLebalBean);
                    Toast.makeText(MainActivity.this, secondLeftLebalBean.getTxt(), Toast.LENGTH_SHORT).show();

                } else {
                    selectedLeftId = vipShareSecondLeftLebalBeans.indexOf(secondLeftLebalBean);
                    rightLebalBeans1Confirm.clear();
                    rightLebalBeans2Confirm.clear();
                    rightLebalBeans1Confirm.addAll(rightLebalBeans1);
                    rightLebalBeans2Confirm.addAll(rightLebalBeans2);
                    Toast.makeText(MainActivity.this,
                            secondLeftLebalBean.getTxt() + "  "
                                    + secondRightLebalBean.getTxt(), Toast.LENGTH_SHORT).show();
                }
            }


        });
        // 一级：左侧按钮 二级 右侧按钮
        // 一级标签 数据

        List<VipShareFirstLebalBean> vipShareFirstLebalBeans = new ArrayList<>();
        for (
                int i = 0;
                i < 10; i++) {
            VipShareFirstLebalBean tmp = new VipShareFirstLebalBean();
            tmp.setTxt("一级标签" + i);
            vipShareFirstLebalBeans.add(tmp);
        }
        // 一级标签
        levelTabbar.setShareFirstLebalBeans(vipShareFirstLebalBeans);

        // 二级标签 的 左侧标签 数据
        vipShareSecondLeftLebalBeans = new ArrayList<>();
        for (
                int i = 0;
                i < 10; i++) {
            VipShareSecondLeftLebalBean tmp = new VipShareSecondLeftLebalBean();
//            // 默认选第一个
//            tmp.setSelected(i == 0);
            tmp.setTxt("二级标签左" + i);
            vipShareSecondLeftLebalBeans.add(tmp);
        }
        // 二级标签 的 左侧标签
        levelTabbar.setSecondLeftLebalBeans(vipShareSecondLeftLebalBeans);

        setSecondRightTags(0);


    }

    private void setSecondRightTags(int left) {

        // 二级标签 的 右侧标签
        // 注意：需要UniID来保证它的唯一性
        String[] mVals = new String[]
                {"二级标签右侧" + left, "二级标签" + left, "二级标签" + left, "二级标签右侧" + left, "二级标签右侧" + left,
                        "二级标签" + left, "二级标签一级标签一级标签" + left, "二级标签右侧" + left};
        rightLebalBeans1.clear();
        rightLebalBeans2.clear();

        // 上部分 数据
        for (int i = 0; i < mVals.length; i++) {
            VipShareSecondRightLebalBean temp = new VipShareSecondRightLebalBean();
            temp.setUniId(left + "rightLebalBeans1" + i);
            temp.setTxt(mVals[i]);
            rightLebalBeans1.add(temp);
        }
        // 下部分 数据
        for (int i = 0; i < mVals.length; i++) {
            VipShareSecondRightLebalBean temp = new VipShareSecondRightLebalBean();
            temp.setUniId(left + "rightLebalBeans2" + i);
            temp.setTxt(mVals[i]);
            rightLebalBeans2.add(temp);
        }
        rightTabs[0]="";
        rightTabs[1]="";

        if (left == 2) { rightTabs[0]="二级标签"+left;
            levelTabbar.setSecondRightLebalBeansAll(rightLebalBeans1, null);
        } else if (left == 4) {
            rightTabs[1]="产品"+ left;

            levelTabbar.setSecondRightLebalBeansAll(null, rightLebalBeans2);
        } else {
            rightTabs[0]="二级标签"+left;

            rightTabs[1]="产品"+ left;


            // 二级标签 右侧 arg1 上部分 arg2 下部分
            levelTabbar.setSecondRightLebalBeansAll(rightLebalBeans1, rightLebalBeans2);
        }
        // 二级标签 ，上下两个文字

        levelTabbar.setRightLabelStr(rightTabs[0], rightTabs[1] );

    }


}